package com.zack.web;

import com.zack.dao.UserDao;
import com.zack.domain.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zack
 * @create 2019-08-06-19:36
 */
@WebServlet(urlPatterns = "/loginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置页面编码
        req.setCharacterEncoding("UTF-8");
        //根据属性名获取属性值
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //新建一个Userdao 对象
        UserDao userDao = new UserDao();
        //新建一个用户
        User loginuser = new User();
        //为新建的用户设置账号密码
        loginuser.setUsername(username);
        loginuser.setPassword(password);
        //Userdao方法来测试该用户的账号密码
        User user = userDao.login(loginuser);
        //结果判断
        if (user == null) {
            req.getRequestDispatcher("/failServlet").forward(req, resp);
        } else {
            //如果成功的话就把user存到req域中
            req.setAttribute("user", user);
            req.getRequestDispatcher("/successServlet").forward(req, resp);
        }


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
